dnl Process this file with autoconf to produce a configure script.
dnl Created by Anjuta application wizard.
AC_PREREQ(2.13)
AC_INIT(bsp, 0.1)
m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])])

AM_INIT_AUTOMAKE(AC_PACKAGE_NAME, AC_PACKAGE_VERSION)
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIR([m4])
AM_MAINTAINER_MODE

AC_ISC_POSIX
AC_PROG_CC
AM_PROG_CC_STDC
AC_HEADER_STDC
AC_PROG_CXX
AC_PROG_RANLIB
AM_PROG_CC_C_O
AM_PROG_LIBTOOL

AC_SEARCH_LIBS(socket, socket)
AC_SEARCH_LIBS(gethostbyname, nsl)
AC_SEARCH_LIBS(mallinfo, malloc)
AC_SEARCH_LIBS(pthread_create, pthread)
AC_SEARCH_LIBS(log2, m)
AC_SEARCH_LIBS(dlopen, dl)
AC_SEARCH_LIBS(lua_close, lua)
AC_SEARCH_LIBS(newwin, ncurses)

AC_CHECK_FUNCS([dup2 \
    gettimeofday \
    memmove \
    memset \
    realpath \
    socket \
    strdup \
    strrchr \
    strstr
])

AC_CHECK_HEADERS([ \
    errno.h \
    fcntl.h \
    limits.h \
    malloc.h \
    math.h \
    curses.h \
    stdlib.h \
    unistd.h \
    pthread.h \
    time.h \
    sched.h \
    arpa/inet.h \
    netinet/in.h \
    sys/epoll.h \
    sys/stat.h \
    sys/time.h \
    sys/socket.h \
    sys/syscall.h \
    sys/uio.h
], [], [], [
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
])

AC_FUNC_FORK
AC_FUNC_MALLOC
AC_FUNC_REALLOC
AC_TYPE_SIZE_T

# Merge all include path
CPPFLAGS="-I$PWD/ -I$PWD/src/ -I$PWD/src/libs/app/ -I$PWD/src/libs/core/ -I$PWD/src/libs/status/ -I$PWD/src/libs/storage/ $CPPFLAGS"

# Check lua (header & lib)
trylibluadir=""
AC_ARG_WITH([lua],
    [AS_HELP_STRING([--with-lua=PATH], [Specified path of liblua installation])], 
    [
         if test "x$withval" != "xno"; then
            trylibluadir=$withval
        fi
    ]
)

trylibluainc=""
AC_ARG_WITH([luainc], 
    [AS_HELP_STRING([--with-luainc=PATH], [Specified path of liblua-devel installation])], 
    [
        if test "x$withval" != "xno"; then
            trylibluainc=$withval
        fi
    ]
)

AC_CACHE_CHECK([for liblua directory], ac_cv_liblua_dir, [
    saved_LIBS="$LIBS"
    saved_LDFLAGS="$LDFLAGS"
    saved_CPPFLAGS="$CPPFLAGS"
    ll_found=no
    for lldir in $trylibluadir "" /usr /usr/local; do
        if test ! -z "$lldir" -a ! -d "$lldir"; then
            continue;
        fi
        for sdir in "" /lib /lib64 /lib32; do
            for llink in lua lua5.1; do
                LIBS="$saved_LIBS -l$llink"
                LDFLAGS="-L$lldir$sdir $saved_LDFLAGS"
                
                for lidir in $trylibluainc $lldir "" /usr /usr/local; do
                    CPPFLAGS="$saved_CPPFLAGS"
                    if test -f "$lidir/include/lua.h"; then
                        CPPFLAGS="-I$lidir/include $CPPFLAGS"
                    elif test -f "$lidir/include/lua5.1/lua.h"; then
                        CPPFLAGS="-I$lidir/include/lua5.1 $CPPFLAGS"
                    elif test -f "$lidir/lua.h"; then
                        CPPFLAGS="-I$lidir $CPPFLAGS"
                    elif test -f "$lidir/lua5.1/lua.h"; then
                        CPPFLAGS="-I$lidir/lua5.1 $CPPFLAGS"
                    fi
                    
                    AC_TRY_LINK([#include <lua.h>
                                 #include <lauxlib.h>
                                 #include <stdio.h>], 
                                 [lua_State *L = luaL_newstate(); lua_close(L);],
                                 [liblua_linked=yes], 
                                 [liblua_linked=no])
                    
                    if test $liblua_linked = yes; then
                        if test ! -z "$lldir"; then
                            ac_cv_liblua_dir=$lldir
                        else
                            ac_cv_liblua_dir="(system)"
                        fi
                        ll_found=yes
                        break
                    fi
                done
                if test $ll_found = yes; then
                    break
                fi
            done
            if test $ll_found = yes; then
                break
            fi
        done
        if test $ll_found = yes; then
            break
        fi
    done
    
    if test $ll_found = no; then
        AC_MSG_ERROR([liblua is required!])
        LIBS="$saved_LIBS"
        LDFLAGS="$saved_LDFLAGS"
        CPPFLAGS="$saved_CPPFLAGS"
    fi
])

# Check mysql (header & lib) with mysql_config
trymysqlconfig="mysql_config"
AC_ARG_WITH([mysql_config], 
    [AS_HELP_STRING([--with-mysql-config=FILE], [Specified of mysql_config])], 
    [
        if test "x$withval" != "xno"; then
            trymysqlconfig=$withval
        fi
    ]
)

AC_CACHE_CHECK([for mysqlclient installation], ac_cv_mysqlclient_dir, [
    saved_LIBS="$LIBS"
    saved_LDFLAGS="$LDFLAGS"
    saved_CPPFLAGS="$CPPFLAGS"
    lm_found=no
    
    LIBS=`$trymysqlconfig --libs`" $LIBS"
    CPPFLAGS=`$trymysqlconfig --include`" $CPPFLAGS"
    AC_TRY_LINK([#include <stdlib.h>
                #include <mysql.h>], 
                [mysql_init(NULL);], 
                [libmysqlclient_linked=yes], 
                [libmysqlclient_linked=no])
    if test $libmysqlclient_linked = yes; then
        ac_cv_mysqlclient_dir="$trymysqlconfig"
        lm_found=yes
    fi
    
    if test $lm_found = no; then
        LIBS="$saved_LIBS"
        LDFLAGS="$saved_LDFLAGS"
        CPPFLAGS="$saved_CPPFLAGS"
        AC_MSG_ERROR([mysql_config is required!])
    fi
])

# Check Berkerly DB
trylibdbdir=""
AC_ARG_WITH([db],
    [AS_HELP_STRING([--with-db=PATH], [Specified path of libdb *BerkerlyDB* installation])], 
    [
        if test "x$withval" != "xno"; then
            trylibdbdir=$withval
        fi
    ]
)

trylibdbinc=""
AC_ARG_WITH([dbinc], 
    [AS_HELP_STRING([--with-dbinc=PATH], [Specified path of libdb-devel *BerkerlyDB* installation])], 
    [
        if test "x$withval" != "xno"; then
            trylibdbinc=$withval
        fi
    ]
)

AC_CACHE_CHECK([for libdb directory], ac_cv_libdb_dir, [
    saved_LIBS="$LIBS"
    saved_LDFLAGS="$LDFLAGS"
    saved_CPPFLAGS="$CPPFLAGS"
    ld_found=no
    LIBS="$saved_LIBS -ldb"
    for dldir in $trylibdbdir "" /usr /usr/local; do
        if test $ld_found = yes; then
            break
        fi

        if test ! -z "$dldir" -a ! -d "$dldir"; then
            continue;
        fi
        
        for sdir in "" /libb /lib64 /lib32; do
            LDFLAGS="-L$dldir/$sdir $saved_LDFLAGS"
            for didir in $trylibdbinc $dldir "" /usr /usr/local; do
                CPPFLAGS="$saved_CPPFLAGS"
                if test -f "$didir/include/db.h"; then
                    CPPFLAGS="-I$didir/include $CPPFLAGS"
                else
                    if test -f "$didir/db.h"; then
                        CPPFLAGS="-I$didir $CPPFLAGS"
                    else
                        continue;
                    fi
                fi
                AC_TRY_LINK([#include <db.h>
                             #include <stdio.h>], 
                             [DB *dbp; db_create(&dbp, NULL, 0);],
                             [libdb_linked=yes], 
                             [libdb_linked=no])
                if test $libdb_linked = yes; then
                    if test ! -z "$dldir"; then
                        ac_cv_libdb_dir=$dldir
                    else
                        ac_cv_libdb_dir="(system)"
                    fi
                    ld_found=yes
                    break;
                fi
            done
            if test $ld_found = yes; then
                break;
            fi
        done
        if test $ld_found = yes; then
            break;
        fi
    done
    
    if test $ld_found = no; then
        LIBS="$saved_LIBS"
        LDFLAGS="$saved_LDFLAGS"
        CPPFLAGS="$saved_CPPFLAGS"
        AC_MSG_ERROR([BerkerlyDB is required!])
    fi
])

dnl Check if we're a little-endian or a big-endian system, needed by hash code
AC_DEFUN([AC_C_ENDIAN],
[AC_CACHE_CHECK(for endianness, ac_cv_c_endian,
[
    AC_RUN_IFELSE(
        [AC_LANG_PROGRAM([], [dnl
            long val = 1;
            char *c = (char *) &val;
            exit(*c == 1);
        ])
    ],[
        ac_cv_c_endian=big
    ],[
        ac_cv_c_endian=little
    ])
])
if test $ac_cv_c_endian = big; then
    AC_DEFINE(ENDIAN_BIG, 1, [machine is bigendian])
fi
if test $ac_cv_c_endian = little; then
    AC_DEFINE(ENDIAN_LITTLE, 1, [machine is littleendian])
fi
])

AC_C_ENDIAN

#Check eventfd support
AC_CACHE_CHECK(for eventfd support, ac_cv_eventfd_linked, 
[
    AC_TRY_LINK([#include <sys/eventfd.h>], 
                [int fd = eventfd(0, EFD_NONBLOCK);], 
                [ac_cv_eventfd_linked=yes], 
                [ac_cv_eventfd_linked=no])
])
if test $ac_cv_eventfd_linked = yes; then
    AC_DEFINE(HAVE_EVENTFD, 1, [Eventfd supported by current kernel])
fi

#Check timerfd support
AC_CACHE_CHECK(for timerfd support, ac_cv_timerfd_linked, 
[
    AC_TRY_LINK([#include <sys/timerfd.h>], 
                [int fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK);], 
                [ac_cv_timerfd_linked=yes], 
                [ac_cv_timerfd_linked=no])
])
if test $ac_cv_timerfd_linked = yes; then
    AC_DEFINE(HAVE_TIMERFD, 1, [Timerfd supported by current kernel])
fi

AC_CHECK_SIZEOF(size_t, 8)
AC_CHECK_SIZEOF(long long, 8)
AC_CHECK_SIZEOF(long long int 8)
AC_CHECK_SIZEOF(long, 8)
AC_CHECK_SIZEOF(int, 4)

AC_OUTPUT([
Makefile
src/Makefile
src/exts/Makefile
src/exts/cache/Makefile
src/exts/http/Makefile
src/exts/luabase/Makefile
src/exts/as3sandbox/Makefile
src/exts/status/Makefile
src/support/Makefile
])
